<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/10/16
 * Time: 17:34
 */
 
 namespace HiCommon\Repository;

use HiCommon\Model\DiscoverApprise;

class DiscoverAppriseRepository extends BaseRepository
{
    public function base_model(){
        return new \HiCommon\Model\DiscoverApprise();
    }

    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all($arr_option = [])
    {
        return DiscoverApprise::commonget($arr_option);
    }

    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list($arr_option = [] , array $arr_search = [] )
    {
        return DiscoverApprise::where(function ($query)
            use($arr_search) {
                if(!empty($arr_search['title'])){
                    $query->where('title', 'like', '%'.$arr_search['title'].'%');
                }
                if(!empty($arr_search['pid'])){
                    $query->where('pid',$arr_search['pid']);
                }
        })->commonpage($arr_option);
    }
    
    /**
     * 通过where分页获取数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_by_where($arr_where,$arr_option = [])
    {
        return DiscoverApprise::where($arr_where)->commonpage($arr_option);

    }
    
    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where($arr_where)
    {
        return DiscoverApprise::where($arr_where)->count();
    }
    
    /**
     * 根据条件统计总和
     *
     * @param array  $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_sum_by_where($arr_where = [],$s_field = 'mark')
    {
        return DiscoverApprise::where($arr_where)->sum($s_field);
    }
    
    /**
     * 根据条件批量删除信息
     *
     * @param array $arr_where
     * @param bool  $method
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_by_where($arr_where = [],$method = false)
    {
        return DiscoverApprise::where($arr_where)
                                    ->commondelete($method);
    }
    
    /**
     * 通过where获取一条数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one_by_where($arr_where,$arr_option = [])
    {
        return DiscoverApprise::where($arr_where)->commonfirst($arr_option);
    }

    /**
     * 添加一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        return DiscoverApprise::create($arr_input);
    }
    
    /**
     * 添加多条数据
     *
     * @param array $arr_data
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_many($arr_data)
    {
        return DiscoverApprise::insert($arr_data);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id, $arr_option = [])
    {
        return DiscoverApprise::commonfind($n_id, $arr_option);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function find_one(int $n_id)
    {
        return DiscoverApprise::find($n_id);
    }
    
    /**
     * 更新或新建一条数据
     *
     * @param $arr_where
     * @param $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_or_create($arr_where,$arr_input){
        $res = DiscoverApprise::where($arr_where)->first();
        if(is_null($res)){
            DiscoverApprise::create(array_merge($arr_where,$arr_input));
        }
        else{
            DiscoverApprise::where($arr_where)->update($arr_input);
        }
        return DiscoverApprise::where($arr_where)->first();
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        return DiscoverApprise::where('id',$arr_input['id'])->update($arr_input);
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return DiscoverApprise::where('id', $n_id)
            ->commondelete();
    }


    /**
     * 根据指定条件删除数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one_where( array $where )
    {
        return DiscoverApprise::where( $where )->commondelete( true );
    }


    /**
     * 根据指定的条件修改指定字段的参数
     *
     * @param int $n_id
     * @param int $n_state
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_state_to_where(array $where = [] , $s_field = 'is_read' , $n_step = 1 )
    {
        return DiscoverApprise::where( $where )->update([$s_field=>$n_step]);

    }
    
    /**
     * 字段自增
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return DiscoverApprise::where('id',$n_id)->increment($s_field,$n_step);
    }

    /**
     * 字段自减
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return DiscoverApprise::where('id',$n_id)->decrement($s_field,$n_step);
    }


}